//
//  XMInfoEditView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/14.
//

#import "XMInfoEditView.h"
#import "XMChangeNameView.h"
#import "XMintroEditView.h"
#import "XMSexSelectView.h"
#import "XMSchoolSelectView.h"

@interface XMInfoEditView ()

@property (nonatomic, strong) XMChangeNameView *changeNameView;
@property (nonatomic, strong) XMintroEditView *introEditView;
@property (nonatomic, strong) XMSexSelectView *sexSelectView;
@property (nonatomic, strong) XMSchoolSelectView *schoolSelectView;

@property (nonatomic, strong) XMUserInfoModel *userInfoModel;

@end

@implementation XMInfoEditView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navView.backgroundColor = kColorHEX(0xF0F0F0);
    self.navTitleLabel.text = @"编辑名字";
    
    self.backgroundColor = kColorHEX(0xF0F0F0);
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(self.width - KScaleWidth(8.0) - KScaleWidth(55.0), kNavagationBarH - 44 + (44 - KScaleWidth(24.0))/2, KScaleWidth(55.0), KScaleWidth(24.0)) title:@"保存" titleColor:kColorHEX(0x333333)];
    [saveBtn setRadius:saveBtn.height/2 borderColor:kColorHEX(0x979797) borderWidth:1];
    [saveBtn addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.navView addSubview:saveBtn];
}

- (void)saveAction:(UIButton *)button {
    if (_changeNameView) {
        _userInfoModel.nickname = _changeNameView.nameTextField.text;
    }
    if (_introEditView) {
        _userInfoModel.dsc = _introEditView.introTextView.text;
    }
    self.saveBlock ? self.saveBlock() : nil;
}

- (void)setContentWithUserInfoModel:(XMUserInfoModel *)userInfoModel
                              index:(NSInteger)index {
    _userInfoModel = userInfoModel;
    switch (index) {
        case 0: {
            self.navTitleLabel.text = @"编辑名字";
            [self addSubview:self.changeNameView];
            [_changeNameView setContentWithName:userInfoModel.nickname];
        }
            break;
        case 1: {
            self.navTitleLabel.text = @"编辑ID号";
//            [_introEditView setcontentwith]
        }
            break;
        case 2: {
            self.navTitleLabel.text = @"编辑简介";
            [self addSubview:self.introEditView];
            [_introEditView setContentWithDes:userInfoModel.dsc];
        }
            break;
        case 3: {
            self.navTitleLabel.text = @"编辑性别";
            [self addSubview:self.sexSelectView];
        }
            break;
        case 4: {
            
        }
            break;
        case 5: {
            
        }
            break;
        case 6: {
            self.navTitleLabel.text = @"编辑学校";
            [self addSubview:self.schoolSelectView];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - Init

- (XMChangeNameView *)changeNameView {
    if (!_changeNameView) {
        _changeNameView = [[XMChangeNameView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, KScaleWidth(150.0))];
    }
    return _changeNameView;
}

- (XMintroEditView *)introEditView {
    if (!_introEditView) {
        _introEditView = [[XMintroEditView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, KScaleWidth(180.0))];
    }
    return _introEditView;
}

- (XMSexSelectView *)sexSelectView {
    if (!_sexSelectView) {
        _sexSelectView = [[XMSexSelectView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, KScaleWidth(150.0))];
        
        XMWeakSelf;
        _sexSelectView.sexSelectBlock = ^(NSInteger sexIndex) {
            weakSelf.userInfoModel.sex = [NSString stringWithFormat:@"%ld", sexIndex];
            weakSelf.saveBlock ? weakSelf.saveBlock() : nil;
        };
    }
    return _sexSelectView;
}

- (XMSchoolSelectView *)schoolSelectView {
    if (!_schoolSelectView) {
        _schoolSelectView = [[XMSchoolSelectView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, self.height - kNavagationBarH)];
    }
    return _schoolSelectView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
